package net.fxug.hokuriku.chocolat.helper
{
    import flash.events.Event;
    
    import mx.core.IMXMLObject;
    import mx.events.FlexEvent;
    
    import net.fxug.hokuriku.chocolat.model.SessionTimer;
    import net.fxug.hokuriku.chocolat.view.DigitalTimerControl;

    /**
     * タイマーコントロール(View)のヘルパークラス
     * 
     * @author shoito
     */
    public class DigitalTimerControlHelper implements IMXMLObject
    {
        private var view:DigitalTimerControl

        private var sessionTimer:SessionTimer;
        
        private var timeFormatter:TimeFormatter;

        public function initialized(document:Object, id:String):void
        {
            this.view = document as DigitalTimerControl;
            this.view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
            
            timeFormatter = new TimeFormatter();
        }

        private function creationCompleteHandler(event:FlexEvent):void
        {
            this.sessionTimer = SessionTimer.getInstance();
            this.sessionTimer.addEventListener(Event.CHANGE, sessionTimer_changeHandler);
			
			//TODO vote監視開始

            sessionTimer_changeHandler();
        }

        private function sessionTimer_changeHandler(event:Event = null):void
        {
        	this.view.time.text = timeFormatter.format(sessionTimer.remainingTime);
        }
    }
}